package com.atguigu.dga230301.governance.service.impl;

import com.atguigu.dga230301.governance.bean.GovernanceAssessTable;
import com.atguigu.dga230301.governance.bean.GovernanceType;
import com.atguigu.dga230301.governance.mapper.GovernanceAssessTableMapper;
import com.atguigu.dga230301.governance.service.GovernanceAssessTableService;
import com.atguigu.dga230301.governance.service.GovernanceTypeService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 表治理考评情况 服务实现类
 * </p>
 *
 * @author zhangjunyi
 * @since 2023-07-05
 */
@Service
@DS("dga")
public class GovernanceAssessTableServiceImpl extends ServiceImpl<GovernanceAssessTableMapper, GovernanceAssessTable> implements GovernanceAssessTableService {

    @Autowired
    GovernanceAssessTableMapper governanceAssessTableMapper;

    @Autowired
    GovernanceTypeService governanceTypeService;

    @Override
    public void calcGovernanceAssessTable(String assessDate) {
        //每次重跑之前先删除当天的数据
        remove(new QueryWrapper<GovernanceAssessTable>().eq("assess_date", assessDate));

        List<GovernanceAssessTable> governanceAssessTableList = governanceAssessTableMapper.selectGovernanceAssessTable(assessDate);
        //获取到每个模块的权重
        List<GovernanceType> governanceTypeList = governanceTypeService.list();
        //创建Map集合存放没个模块的权重  k：type_code v:score
        HashMap<String, BigDecimal> governanceTypeMap = new HashMap<>();
        for (GovernanceType governanceType : governanceTypeList) {
            governanceTypeMap.put(governanceType.getTypeCode(), governanceType.getTypeWeight());
        }

        //根据不同模块乘以权重计算分组
        for (GovernanceAssessTable governanceAssessTable : governanceAssessTableList) {
            BigDecimal specScore =    (governanceAssessTable.getScoreSpecAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreSpecAvg()).multiply(governanceTypeMap.get("SPEC")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal storageScore =  (governanceAssessTable.getScoreStorageAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreStorageAvg()).multiply(governanceTypeMap.get("STORAGE")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal calcScore =  (governanceAssessTable.getScoreCalcAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreCalcAvg()).multiply(governanceTypeMap.get("CALC")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal qualityScore =  (governanceAssessTable.getScoreQualityAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreQualityAvg()).multiply(governanceTypeMap.get("QUALITY")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal securityScore =  (governanceAssessTable.getScoreSecurityAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreSecurityAvg()).multiply(governanceTypeMap.get("SECURITY")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal score = specScore.add(storageScore).add(calcScore).add(qualityScore).add(securityScore).movePointLeft(2);
            governanceAssessTable.setScoreOnTypeWeight(score);
            governanceAssessTable.setCreateTime(new Date());
        }

        //保存到mysql
        saveBatch(governanceAssessTableList);
    }
}
